package com.yex.service.otc.impl;


import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yex.common.utils.DateUtil;
import com.yex.dao.otc.OtcDayAmountDao;
import com.yex.utils.vo.RespCode;
import com.yex.utils.JsonResultVo;
import com.yex.entity.otc.OtcDayAmount;
import com.yex.base.ZeroResultException;
import com.yex.service.otc.IOtcDayAmountService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 每日交易金额 服务实现类
 * </p>
 *
 * @author LittleLiu
 * @since 2018-07-03
 */
@Service
public class OtcDayAmountServiceImpl extends ServiceImpl<OtcDayAmountDao, OtcDayAmount> implements IOtcDayAmountService {
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResultVo saveOtcDayAmount(Integer userId, BigDecimal amount) {
        OtcDayAmount otcDayAmount = baseMapper.findOtcDayAmount(userId);
        if (otcDayAmount == null) {
            Integer result = baseMapper.saveOtcDayAmount(userId,amount);
//            Integer result =  jdbc.jdbcTemplate().update("insert into  otc_day_amount(`userId`, `amount`, `push`, `modifyDate`, `version`) select ?, ?, '0', NOW(),0\n" +
//                    "        from dual where not exists (SELECT id FROM otc_day_amount WHERE  userId=?)",userId,amount,userId);
            if (result <= 0) {
                throw new ZeroResultException(new JsonResultVo(RespCode.FAILURE.getMsg()));
            }
        } else {
            String old = DateUtil.format(DateUtil.ZH_CN_DATE, otcDayAmount.getModifyDate());
            String now = DateUtil.format(DateUtil.ZH_CN_DATE, new Date());
            if(old.equals(now)){
                Integer result = baseMapper.updateOtcDayAmount(userId,amount);
                if (result <= 0) {
                    throw new ZeroResultException(new JsonResultVo(RespCode.FAILURE.getMsg()));
                }
            }else {
                Integer result = baseMapper.updateOtcDayAmountZERO(userId,amount);
                if (result <= 0) {
                    throw new ZeroResultException(new JsonResultVo(RespCode.FAILURE.getMsg()));
                }
            }
        }
        return new JsonResultVo(true,"成功",0);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResultVo saveOtcDayAmountPush(Integer userId) {
        OtcDayAmount otcDayAmount = baseMapper.findOtcDayAmount(userId);
        if (otcDayAmount == null) {
            Integer result = baseMapper.saveOtcDayAmountPush(userId);
//            Integer result =  jdbc.jdbcTemplate().update("insert into  otc_day_amount(`userId`, `amount`, `push`, `modifyDate`, `version`) select ?, '0', '1', NOW(),0\n" +
//                    "        from dual where not exists (SELECT id FROM otc_day_amount WHERE  userId=?)",userId,userId);
            if (result <= 0) {
                throw new ZeroResultException(new JsonResultVo(RespCode.FAILURE.getMsg()));
            }
        } else {
            Integer result = baseMapper.updateOtcDayAmountPush(userId);
            if (result <= 0) {
                throw new ZeroResultException(new JsonResultVo(RespCode.FAILURE.getMsg()));
            }
        }
        return new JsonResultVo(true,"成功",0);
    }

    @Override
    public JsonResultVo findOtcDayAmount(Integer userId) {
        Map<String, Object> otcDayAmount = baseMapper.findOtcDayAmountMap(userId);
        return new JsonResultVo(true,"成功",otcDayAmount,0);

    }
}
